package com.leo;

import javafx.animation.Animation;
import javafx.animation.PathTransition;
import javafx.animation.Transition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @ClassName TranslateTransition
 * @Description
 * @Author wangss
 * @date 2020.10.21 21:13
 * @Version 1.0
 */
public class PathTransitionDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox(50);
        ToggleGroup tg = new ToggleGroup();

        ToggleButton tb1 = new ToggleButton("播放");
        tb1.setToggleGroup(tg);

        ToggleButton tb2 = new ToggleButton("暂停");
        tb2.setToggleGroup(tg);

        ToggleButton tb3 = new ToggleButton("停止");
        tb3.setToggleGroup(tg);

        ToggleButton tb4 = new ToggleButton("跳转");
        tb4.setToggleGroup(tg);

        Rectangle rec = new Rectangle(100, 100, Color.RED);
        rec.setX(-50);
        rec.setY(-50);
//        rec.setTranslateX(-49);
//        rec.setTranslateY(-49);

        PathTransition tt = new PathTransition();
        // 跟随路径方向转动
        tt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
        tt.setDuration(Duration.seconds(3));
        tt.setNode(rec);
        tt.setAutoReverse(true);
        tt.setCycleCount(Animation.INDEFINITE);

        Path path = new Path();

        MoveTo moveTo = new MoveTo(0, 0);

        LineTo lineTo = new LineTo(100, 0);

        QuadCurveTo qct = new QuadCurveTo(0, 0, 100, 100);
        // 相对位置
        qct.setAbsolute(false);

        HLineTo hLineTo = new HLineTo(100);
        hLineTo.setAbsolute(false);

        CubicCurveTo curveTo = new CubicCurveTo(50, -50, 150, 50, 200, 0);
        curveTo.setAbsolute(false);

        VLineTo vLineTo = new VLineTo(100);
        vLineTo.setAbsolute(false);

        ArcTo arcTo = new ArcTo(100, 100, 0, 100, 100, true, true);
        arcTo.setAbsolute(false);

        ClosePath closePath = new ClosePath();

        path.getElements().addAll(moveTo, lineTo, qct, hLineTo, curveTo, vLineTo, arcTo, closePath);
        path.setStrokeWidth(2);
        path.setStroke(Color.BLUE);

        tt.setPath(path);

        StackPane sp = new StackPane();
        sp.getChildren().add(path);

        hBox.getChildren().addAll(tb1, tb2, tb3, tb4);
        AnchorPane an = new AnchorPane();
        an.getChildren().addAll(hBox, rec, sp);
        AnchorPane.setTopAnchor(hBox, 100d);
        AnchorPane.setLeftAnchor(hBox, 100d);
        AnchorPane.setTopAnchor(rec, 300d);
        AnchorPane.setLeftAnchor(rec, 100d);
        AnchorPane.setTopAnchor(sp, 300d);
        AnchorPane.setLeftAnchor(sp, 100d);
        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ToggleButton");
        primaryStage.setWidth(1200d);
        primaryStage.setHeight(800d);
        primaryStage.setAlwaysOnTop(true);
        primaryStage.show();

        addAction(tt, tb1, tb2, tb3, tb4);
    }

    private void addAction(Transition timeline, ToggleButton tb1, ToggleButton tb2, ToggleButton tb3, ToggleButton tb4) {
        tb1.setOnAction(event -> {
            timeline.play();
        });

        tb2.setOnAction(event -> {
            timeline.pause();
        });

        tb3.setOnAction(event -> {
            timeline.stop();
        });

        tb4.setOnAction(event -> {

        });
    }
}
